﻿using GWB.Application;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Threading.Tasks;
using System.Linq;
using System.Net.Http;

namespace GWB.Utils.Common
{
    /// <summary>
    /// IExceptionFilter 异常拦截
    /// ActionFilterAttribute 请求拦截器
    /// </summary>
    public class ActionAttribute : Attribute, IActionFilter
    {
        public ILogService _logService;
        public ActionAttribute(ILogService logService) 
        {
            _logService = logService;
        }
        public void OnActionExecuted(ActionExecutedContext context)
        {
            var v1 = context.HttpContext.Request.Headers["Cdn-Src-Ip"];       // '获取服务器IP
            var v2 = context.HttpContext.Request.Headers["Cdn-Src-Ip"].FirstOrDefault();

            // 获取客户端的 IP 地址
            string clientIp = context.HttpContext.Connection.RemoteIpAddress?.ToString();
            // 获取客户端的浏览器信息
            string userAgent = context.HttpContext.Request.Headers["User-Agent"];

            var data = new object();
            data = new
            {
                v1 = "Ip：" + clientIp,
                v2 = "浏览器：" + userAgent,
            };

            // 返回结果为JsonResult的请求进行Result包装
            if (context.Result != null)
            {
                if (context.Result is ObjectResult)
                {
                    var result = context.Result as ObjectResult;
                    context.Result = new JsonResult(new { code = 200, data1 = data, msg = "success", data = result.Value });

                }
                else if (context.Result is EmptyResult)
                {
                    context.Result = new JsonResult(new { code = 200, msg = "success", data1 = data, data = new { } });
                }
                else if (context.Result is ContentResult)
                {
                    var result = context.Result as ContentResult;
                    context.Result = new JsonResult(new { code = result.StatusCode, data2 = data, msg = result.Content });
                }

            }

        }

        public void OnActionExecuting(ActionExecutingContext context)
        {

          

            // Log(filterContext);
        }

        private void Log(ActionExecutingContext filterContext) 
        {
            var controllerName = (string)filterContext.RouteData.Values["controller"];
            var actionName = (string)filterContext.RouteData.Values["action"];
            if (string.IsNullOrWhiteSpace(controllerName) || string.IsNullOrWhiteSpace(actionName)) return;
            if (filterContext.HttpContext.Connection.RemoteIpAddress == null) return;
            var ip = filterContext.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();

         

            Task.Run(()=> {
                _logService.InsertReturnIdentity(new SqlSugar.Model.VisitorLog()
                { CreateTime = DateTime.Now,
                    IP = ip,
                    Route = $"{controllerName}/{actionName}"
                });
            });
        }


    }
}
